package isi_graf;

import java.util.Date;
import java.util.Iterator;

import isi_ib.barra_trade;
import isi_ib.barras_trade;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.data.xy.DefaultHighLowDataset;
import org.jfree.chart.ChartPanel;


public class isiCandelChart {

	private DefaultHighLowDataset dataset;
	private JFreeChart chart;
	private ChartPanel chartPanel;
	String titulo;
	
	
	public  isiCandelChart(String titulo_) {
		super();
		dataset=null;
		chart=null;
		chartPanel=null;
		titulo=titulo_;
	}
	
	public ChartPanel CreaChart(barras_trade barras_){
		dataset = createDataset(barras_);
		chart = createChart(dataset);
		Ajusta(barras_.minvalue,barras_.maxvalue);
		chartPanel = new ChartPanel(chart);
		return chartPanel;
	}
	
	private void Ajusta(Double minvalue_,Double maxvalue_){
		if(chart==null) return;
		if(minvalue_==0 && maxvalue_==0){
			minvalue_ = getLowestLow(dataset);
			maxvalue_ = getHighestHigh(dataset);
		}
		chart.getXYPlot().getRangeAxis().setRange(minvalue_,maxvalue_);
	}
		
	
	
	private JFreeChart createChart(DefaultHighLowDataset dataset_) {
			  if(dataset==null) return null; 
			  JFreeChart chart = ChartFactory.createCandlestickChart(
			  titulo, "Time", "Price", dataset_, false);
			  return chart;
			  }
	private DefaultHighLowDataset createDataset(barras_trade barras_) {

			 
			  Date[] date = new Date[barras_.count()];
			  double[] high = new double[barras_.count()];
			  double[] low = new double[barras_.count()];
			  double[] open = new double[barras_.count()];
			  double[] close = new double[barras_.count()];
			  double[] volume = new double[barras_.count()];



			  //Calendar calendar = Calendar.getInstance();
			  //calendar.set(2008, 5, 1);
			  
			  Iterator<barra_trade> itr = barras_.barras.iterator();
			  int i=0;
			    while (itr.hasNext()) {
			      barra_trade element = itr.next();
			      date[i] =element.getDate();
			      high[i] = element.getHigh();
			      low[i] = element.getLow();
			      open[i] = element.getOpen();
			      close[i] = element.getClose();
			      volume[i]=element.getVolume();
			      i++;
			    }
			 
			  DefaultHighLowDataset data = new DefaultHighLowDataset(
			  "", date, high, low, open, close, volume);
			  return data;
			  }
	private double getLowestLow(DefaultHighLowDataset dataset){
	        double lowest;
	        lowest = dataset.getLowValue(0,0);
	        for(int i=1;i<dataset.getItemCount(0);i++){
	            if(dataset.getLowValue(0,i) < lowest){
	                lowest = dataset.getLowValue(0,i);
	            }
	        }

	        return lowest;
	    }
	private double getHighestHigh(DefaultHighLowDataset dataset){
	        double highest;
	        highest = dataset.getHighValue(0,0);
	        for(int i=1;i<dataset.getItemCount(0);i++){
	            if(dataset.getLowValue(0,i) > highest){
	                highest = dataset.getHighValue(0,i);
	            }
	        }

	        return highest;
	    }   
	
}
